<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/3
 * Time: 16:19
 */
namespace app\data;

use app\common\Common;
use think\Db;

class ShopData extends Base
{
    /**
     * @param $user_id
     * @param $shop_name
     * @param $shop_logo
     * @param $shop_introduce
     * @param $shop_type
     * @param $img
     * @param $subtitle
     * @param $tel
     * @param $type_id
     * @param $id_card
     * @param $shop_user_name
     * @param $shop_we_chat
     * @param $business_license
     * @return array
     */
    public function addShop($user_id, $shop_name, $shop_logo, $shop_introduce,$id_card,$business_license,
                            $shop_type, $type_id, $img, $subtitle, $tel, $shop_user_name, $shop_we_chat)
    {
        $time = time();
        $id = Db::table('drh_shop')
            ->insertGetId([
                'user_id' => $user_id,
                'shop_name' => $shop_name,
                'shop_logo' => $shop_logo,
                'shop_introduce' => $shop_introduce,
                'type_id' => $type_id,
                'shop_type' => $shop_type,
                'img' => $img,
                'subtitle' => $subtitle,
                'create_time' => $time,
                'shop_no' => $tel,
                'shop_user_name' => $shop_user_name,
                'shop_we_chat' => $shop_we_chat,
                'id_card' => $id_card,
                'business_license' => $business_license,
            ]);

        return ['code' => $this->t_code, 'msg' => '添加成功', 'data' => $id];
    }

    /**
     * @param $user_id
     * @param $shop_id
     * @param $shop_name
     * @param $shop_logo
     * @param $shop_introduce
     * @param $type_id
     * @param $shop_type
     * @param $img
     * @param $subtitle
     * @param $shop_user_name
     * @param $shop_we_chat
     * @param $id_card
     * @param $business_license
     * @param $tel
     * @return array
     */
    public function editShop($user_id, $shop_id, $shop_name, $shop_logo, $shop_introduce, $id_card, $business_license,
                             $type_id, $shop_type, $img, $subtitle, $shop_user_name, $shop_we_chat, $tel)
    {
        Db::table('drh_shop')
            ->where([
                'user_id' => $user_id,
                'id' => $shop_id,
            ])
            ->update([
                'shop_name' => $shop_name,
                'shop_logo' => $shop_logo,
                'shop_introduce' => $shop_introduce,
                'type_id' => $type_id,
                'shop_type' => $shop_type,
                'img' => $img,
                'subtitle' => $subtitle,
                'verify_status' => 0,
                'shop_user_name' => $shop_user_name,
                'shop_we_chat' => $shop_we_chat,
                'shop_no' => $tel,
                'id_card' => $id_card,
                'business_license' => $business_license,
            ]);

        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    /**
     * @param $user_id
     * @param $shop_id
     * @return array
     */
    public function delShop($user_id, $shop_id)
    {
        Db::table('drh_shop')
            ->where([
                'user_id' => $user_id,
                'id' => $shop_id,
            ])
            ->update([
                'recover_status' => 1,
            ]);

        return ['code' => $this->t_code, 'msg' => '删除成功'];
    }

    public function shopInfo($shop_id)
    {
        return Db::table('drh_shop a')
            ->join('drh_shop_city b', 'a.id=b.shop_id', 'left')
            ->field([
                'a.total_money',
                'b.city_id',
                'b.province_id',
                'b.district_id',
                'b.latitude',
                'b.longitude',
                'a.integral_return_ratio',
                'a.shop_name',
                'a.shop_logo',
                'a.shop_introduce',
                'a.user_id',
                'a.shop_user_name',
                'a.shop_we_chat',
                'b.address',
                'a.id_card',
                'a.business_license',
                'a.shop_no tel',
                'a.img',
                'a.shop_type',
            ])
            ->where(['a.id' => $shop_id])
            ->find();
    }

    public function showList($page, $pre, $sort, $recover_status = 0)
    {
        $page = ($page - 1) * $pre;
        return Db::table('drh_shop')
            ->field([
                'id',
                'shop_name',
                'shop_logo',
            ])->where([
                'recover_status' => $recover_status,
            ])
            ->order("'".$sort."' desc")
            ->limit($page, $pre)
            ->select();
    }

    /**
     * 获取数量
     * @param $where
     * @param $table
     * @return mixed
     */
    public function total($where, $table = 'drh_shop')
    {
        $res = Db::table($table)
            ->field('count(*) count')
            ->where($where)
            ->find();

        return $res['count'];
    }

    public function showType($sort)
    {
        $sort = Common::spliceArray($sort);

        return Db::table('drh_shop_type')
            ->field([
                'id',
                'type_name',
                'type_img',
            ])
            ->order($sort)
            ->select();
    }

    public function changeShopHot($shop_id, $hot_lv)
    {
        Db::table('drh_shop')
            ->where([
                'shop_id' => $shop_id
            ])->update([
                'hot_lv' => $hot_lv
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    /**
     * 展示分类商户
     * @param $type_id
     * @param $page
     * @param $pre
     * @param $sort
     * @param int $recover_status
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function showTypeShop($type_id, $page, $pre, $sort, $recover_status = 0)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);

        return Db::table('drh_shop')
            ->field([
                'id',
                'shop_name',
                'shop_logo',
                'subtitle',
            ])->where([
                'recover_status' => $recover_status,
                'type_id' => $type_id,
            ])
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    /**
     * 添加商品类别
     * @param $shop_id
     * @param $type_name
     * @param $lv
     * @param $parent_type_id
     * @return array
     */
    public function addGoodsType($shop_id, $type_name, $lv, $parent_type_id = 0)
    {
        $time = time();
        Db::table('drh_goods_type')
            ->insert([
                'type_name' => $type_name,
                'shop_id' => $shop_id,
                'lv' => $lv,
                'parent_type_id' => $parent_type_id,
                'create_time' => $time
            ]);
        return ['code' => $this->t_code, 'msg' => '分类添加成功'];
    }

    /**
     * 修改商品类别
     * @param $shop_id
     * @param $type_name
     * @param $type_id
     * @param $lv
     * @param $parent_type_id
     * @return array
     */
    public function editGoodsType($shop_id, $type_name, $lv, $type_id, $parent_type_id = 0)
    {
        Db::table('drh_goods_type')
            ->where([
                'shop_id' => $shop_id,
                'id' => $type_id,
            ])
            ->update([
                'type_name' => $type_name,
                'lv' => $lv,
                'parent_type_id' => $parent_type_id,
            ]);
        return ['code' => $this->t_code, 'msg' => '分类修改成功'];
    }

    /**
     * 删除商品类别
     * @param $shop_id
     * @param $type_id
     * @return array
     */
    public function delGoodsType($shop_id, $type_id)
    {
        Db::table('drh_goods_type')
            ->where([
                'shop_id' => $shop_id,
                'id' => $type_id
            ])->delete();
        return ['code' => $this->t_code, 'msg' => '删除成功'];
    }

    /**
     * 添加商品轮播图
     * @param $shop_id
     * @param $goods_id
     * @param $img
     * @return array
     */
    public function addTypeRotation($shop_id, $goods_id, $img)
    {
        $time = time();
        Db::table('drh_shop_carousel_img')
            ->insert([
                'shop_id' => $shop_id,
                'goods_id' => $goods_id,
                'create_time' => $time,
                'img' => $img,
            ]);
        return ['code' => $this->t_code, 'msg' => '添加成功'];
    }

    public function delTypeRotation($shop_id, $id)
    {
        Db::table('drh_shop_carousel_img')
            ->where([
                'id' => $id,
                'shop_id' => $shop_id
            ])->delete();
        return ['code' => $this->t_code, 'msg' => '删除成功'];
    }

    public function showTypeRotation($shop_id)
    {
        $res = Db::table('drh_shop_carousel_img')
            ->field([
                'id',
                'shop_id',
                'goods_id',
                'img',
            ])->where([
                'shop_id' => $shop_id
            ])->select();
        return ['code' => $this->t_code, 'msg' => 'OK', 'data' => $res];
    }

    /**
     * 校验商品
     * @param $shop_id
     * @param $goods_id
     * @return bool
     */
    public function checkGoods($shop_id, $goods_id)
    {
        $res = Db::table('drh_goods')
            ->field([
                'id'
            ])->where([
                'shop_id' => $shop_id,
                'id' => $goods_id
            ])->find();
        return empty($res) ? false : true;
    }

    /**
     * 显示商品分类
     * @param $shop_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function showGoodsType($shop_id)
    {
        $res = Db::table('drh_goods_type')
            ->field([
                'id',
                'type_name',
            ])->where(" shop_id in (0,{$shop_id})")
            ->select();
        return ['code' => $this->t_code, 'msg' => 'OK', 'data' => $res];
    }

    /**
     * 显示商品列表
     * @param $shop_id
     * @param $page
     * @param $pre
     * @param $sort
     * @param $is_shelves
     * @param $type_id
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function showGoodsList($shop_id, $page, $pre, $sort, $is_shelves, $type_id)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);

        return Db::table('drh_goods')
            ->field([
                'id',
                'goods_name',
                'goods_price',
                'goods_img',
            ])->where([
                'is_shelves' => $is_shelves,
                'shop_id' => $shop_id,
                'goods_type_id' => $type_id,
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    /**
     * 显示商品详情
     * @param $goods_id
     * @param $is_shelves
     * @return array|false|\PDOStatement|string|\think\Model
     */
    public function showGoods($goods_id, $is_shelves = 1)
    {
        return Db::table('drh_goods')
            ->field([
                'id',
                'shop_id',
                'goods_name',
                'goods_price',
                'give_integral',
                'goods_freight',
                'goods_stock',
                'goods_info',
                'goods_attr_value',
                'goods_img'
            ])->where([
                'id' => $goods_id,
                'is_shelves' => $is_shelves
            ])->find();
    }

    /**
     * 添加商品
     * @param $shop_id
     * @param $goods_name
     * @param $goods_price
     * @param $give_integral
     * @param $goods_type_id
     * @param $goods_group
     * @param $is_shelves
     * @param $goods_stock
     * @param $goods_hot
     * @param $goods_new
     * @param $goods_attr_value
     * @param $goods_info
     * @param $goods_img
     * @param $is_proxy
     * @param $goods_freight
     * @return array
     */
    public function addGoods($shop_id, $goods_name, $goods_price, $give_integral, $goods_type_id,
                             $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new,
                             $goods_attr_value, $goods_info, $goods_img, $goods_freight, $is_proxy)
    {
        $time = time();
        $shelves_time = $is_shelves == 1 ? $time : 0;
        Db::table('drh_goods')
            ->insert([
                'shop_id' => $shop_id,
                'goods_name' => $goods_name,
                'goods_price' => $goods_price,
                'give_integral' => $give_integral,
                'goods_type_id' => $goods_type_id,
                'goods_group' => $goods_group,
                'is_shelves' => $is_shelves,
                'goods_stock' => $goods_stock,
                'goods_hot' => $goods_hot,
                'goods_new' => $goods_new,
                'goods_freight' => $goods_freight,
                'goods_attr_value' => $goods_attr_value,
                'goods_info' => $goods_info,
                'goods_img' => $goods_img,
                'shelves_time' => $shelves_time,
                'is_proxy' => $is_proxy,
                'create_time' => $time,
            ]);
        return ['code' => $this->t_code, 'msg' => '添加成功'];
    }

    /**
     * 修改商品
     * @param $id
     * @param $shop_id
     * @param $goods_name
     * @param $goods_price
     * @param $give_integral
     * @param $goods_type_id
     * @param $goods_group
     * @param $is_shelves
     * @param $goods_stock
     * @param $goods_hot
     * @param $goods_new
     * @param $goods_attr_value
     * @param $goods_info
     * @param $goods_img
     * @param $goods_freight
     * @param $is_proxy
     * @return array
     */
    public function editGoods($id, $shop_id, $goods_name, $goods_price, $give_integral, $goods_type_id,
                              $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new,
                              $goods_attr_value, $goods_info, $goods_img, $goods_freight, $is_proxy)
    {
        $time = time();
        $shelves_time = $is_shelves == 1 ? $time : 0;
        Db::table('drh_goods')
            ->where([
                'shop_id' => $shop_id,
                'id' => $id
            ])->update([
                'goods_name' => $goods_name,
                'goods_price' => $goods_price,
                'give_integral' => $give_integral,
                'goods_type_id' => $goods_type_id,
                'goods_group' => $goods_group,
                'is_shelves' => $is_shelves,
                'goods_stock' => $goods_stock,
                'goods_hot' => $goods_hot,
                'goods_new' => $goods_new,
                'goods_attr_value' => $goods_attr_value,
                'goods_info' => $goods_info,
                'goods_img' => $goods_img,
                'shelves_time' => $shelves_time,
                'create_time' => $time,
                'goods_freight' => $goods_freight,
                'is_proxy' => $is_proxy,
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    /**
     * 删除商品
     * @param $id
     * @param $shop_id
     * @return array
     */
    public function delGoods($id, $shop_id)
    {
        $time = time();
        Db::table('drh_goods')
            ->where([
                'shop_id' => $shop_id,
                'id' => $id
            ])->update([
                'is_shelves' => 3,
                'off_shelves_time' => $time,
            ]);
        return ['code' => $this->t_code, 'msg' => '删除成功'];
    }

    public function showAdminGoodsList($shop_id, $page, $pre, $sort, $is_shelves)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('drh_goods')
            ->field([
                'id',
                'shop_id',
                'goods_name',
                'goods_price',
                'give_integral',
                'goods_group',
                'is_shelves',
                'proxy_goods_id',
                'goods_stock',
                'proxy_goods_value',
            ])->where([
                'shop_id' => $shop_id,
                'is_shelves' => $is_shelves
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showAdminGoodsInfo($shop_id, $goods_id)
    {
        return Db::table('drh_goods')
            ->where([
                'shop_id' => $shop_id,
                'id' => $goods_id
            ])->find();
    }

    public function createBargainGoods($shop_id, $goods_id, $bargain_total_money, $bargain_once_money, $title)
    {
        $time = time();
        Db::table('drh_goods_bargain')
            ->insert([
                'goods_id' => $goods_id,
                'shop_id' => $shop_id,
                'bargain_total_money' => $bargain_total_money,
                'bargain_once_money' => $bargain_once_money,
                'title' => $title,
                'create_time' => $time
            ]);
        return ['code' => $this->t_code, 'msg' => '创建成功'];
    }

    public function editBargainGoods($shop_id, $goods_id, $bargain_total_money, $bargain_once_money, $title)
    {
        Db::table('drh_goods_bargain')
            ->where([
                'goods_id' => $goods_id,
                'shop_id' => $shop_id,
            ])
            ->update([
                'bargain_total_money' => $bargain_total_money,
                'bargain_once_money' => $bargain_once_money,
                'title' => $title
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    public function delBargainGoods($shop_id, $goods_id)
    {
        Db::table('drh_goods_bargain')
            ->where([
                'goods_id' => $goods_id,
                'shop_id' => $shop_id,
            ])->delete();
        return ['code' => $this->t_code, 'msg' => '删除成功'];
    }

    public function showBargainGoods($shop_id, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, ' a.');
        return Db::table('drh_goods_bargain a')
            ->join('drh_goods b', 'a.goods_id=b.id', 'left')
            ->field([
                'b.goods_name',
                'a.bargain_total_money',
                'a.bargain_once_money',
                'a.title',
                'a.shop_id',
                'a.goods_id'
            ])->where([
                'a.shop_id' => $shop_id
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showBargainGood($shop_id, $goods_id)
    {
        return Db::table('drh_goods_bargain')
            ->field([
                'bargain_total_money',
                'bargain_once_money',
                'title',
                'shop_id',
                'goods_id'
            ])->where([
                'shop_id' => $shop_id,
                'goods_id' => $goods_id,
            ])->find();
    }

    public function addProxyGoods($shop_id, $info)
    {
        $time = time();
        $shelves_time = $time;
        Db::table('drh_goods')
            ->insert([
                'shop_id' => $shop_id,
                'goods_name' => $info['goods_name'],
                'goods_price' => $info['goods_price'],
                'give_integral' => $info['give_integral'],
                'goods_type_id' => $info['goods_type_id'],
                'goods_group' => $info['goods_group'],
                'is_shelves' => $info['is_shelves'],
                'goods_stock' => $info['goods_stock'],
                'goods_hot' => $info['goods_hot'],
                'goods_new' => $info['goods_new'],
                'goods_freight' => $info['goods_freight'],
                'goods_attr_value' => $info['goods_attr_value'],
                'goods_info' => $info['goods_info'],
                'goods_img' => $info['goods_img'],
                'shelves_time' => $shelves_time,
                'is_proxy' => 0,
                'proxy_goods_id' => $info['id'],
                'create_time' => $time,
            ]);
        return ['code' => $this->t_code, 'msg' => '添加成功'];
    }

    public function showProxyShop($page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, ' a.');
        return Db::table('drh_shop a')
            ->join('drh_goods b', 'a.id=b.shop_id', 'left')
            ->field([
                'a.id',
                'a.shop_name'
            ])->where("b.is_proxy=1 and a.verify_status=1 and a.recover_status=0")
            ->group('a.id')
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showShopTotal()
    {
        $res = Db::table('drh_shop a')
            ->join('drh_goods b', 'a.id=b.shop_id', 'left')
            ->field([
                'count(*)'
            ])->where("b.is_proxy=1")
            ->group('a.id')
            ->select();

        return empty($res) ? 0 : count($res);
    }

    public function showShopProxyGoods($shop_id, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, ' a.');

        return Db::table('drh_goods a')
            ->field([
                'a.id',
                'a.goods_name'
            ])->where("a.is_proxy = 1 and a.shop_id={$shop_id} and a.is_shelves=1")
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showShopProxyGoodsTotal($shop_id)
    {
        $res = Db::table('drh_goods')
            ->field([
                'count(*) count'
            ])->where("is_proxy=1 and shop_id={$shop_id}")
            ->find();
        return empty($res) ? 0 : $res['count'];
    }

    public function cancelGoodsProxy($goods_id)
    {
        Db::table('drh_goods')
            ->where([
                'proxy_goods_id' => $goods_id
            ])->update([
                'is_shelves' => 3
            ]);
    }

    public function editProxyGoods($id, $goods_name, $goods_price, $give_integral, $goods_type_id,
                                   $goods_group, $is_shelves, $goods_stock, $goods_hot, $goods_new,
                                   $goods_attr_value, $goods_info, $goods_img, $goods_freight, $is_proxy)
    {
        $time = time();
        $shelves_time = $is_shelves == 1 ? $time : 0;
        Db::table('drh_goods')
            ->where([
                'proxy_goods_id' => $id
            ])->update([
                'goods_name' => $goods_name,
                'goods_price' => $goods_price,
                'give_integral' => $give_integral,
                'goods_type_id' => $goods_type_id,
                'goods_group' => $goods_group,
                'goods_stock' => $goods_stock,
                'goods_hot' => $goods_hot,
                'goods_new' => $goods_new,
                'goods_attr_value' => $goods_attr_value,
                'goods_info' => $goods_info,
                'goods_img' => $goods_img,
                'shelves_time' => $shelves_time,
                'create_time' => $time,
                'goods_freight' => $goods_freight,
                'is_proxy' => $is_proxy,
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }


    public function getBargainGoodInfo($id)
    {
        return Db::table('drh_user_goods_bargain')
            ->field([
                'id',
                'bargain_total_money',
                'bargain_once_money',
                'title',
                'shop_id',
                'goods_id',
                'bargain_total_num',
                'bargain_num',
            ])->where([
                'id' => $id
            ])->find();
    }

//    public function shopFlow($order_num, $time, $shop_info, $type_status, $money_expenses, $integral_expenses,
//                             $fees, $msg = '')
//    {
//       return Db::table('drh_shop_flow')
//            ->insertGetId([
//                'shop_id' => $shop_info['id'],
//                'start_money' => $shop_info['money'],
//                'start_integral' => $shop_info['integral'],
//                'order_num' => $order_num,
//                'type_status' => $type_status,
//                'money_expenses' => $money_expenses,
//                'integral_expenses' => $integral_expenses,
//                'fees' => $fees,
//                'end_money' => $shop_info['money'] + $money_expenses,
//                'end_integral' => $shop_info['integral'] + $integral_expenses,
//                'create_time' => $time,
//                'msg' => $msg
//            ]);
//    }

    public function shopFlow($order_num, $time, $user_info, $type_status, $money_expenses, $integral_expenses,
                             $fees, $msg = '', $give_integral = 0)
    {
        Db::table('drh_user_flow')
            ->insert([
                'user_id' => $user_info['id'],
                'start_money' => $user_info['user_money'],
                'start_integral' => $user_info['user_integral'],
                'order_num' => $order_num,
                'type_status' => $type_status,
                'money_expenses' => $money_expenses,
                'integral_expenses' => $integral_expenses,
                'end_money' => $user_info['user_money'] + $money_expenses,
                'end_integral' => $user_info['user_integral'] + $integral_expenses - $give_integral,
                'create_time' => $time,
                'fees' => $fees,
                'msg' => $msg,
                'return_integral' => '-' . $give_integral,
            ]);
        return true;
    }

    public function getShopInfo($id)
    {
        return Db::table('drh_shop')
            ->field([
                'id',
                'money',
                'integral',
                'shop_name',
                'user_id'
            ])->where([
                'id' => $id
            ])->find();
    }

    public function endOrder($id, $money, $integral)
    {
        Db::table('drh_user_info')
            ->where([
                'id' => $id
            ])->update([
                'user_money' => $money,
                'user_integral' => $integral
            ]);
        return true;
    }

    public function endGoods($shop_id, $goods_num, $goods_id)
    {
        Db::table('drh_goods')
            ->field(" goods_stock=goods_stock-{$goods_num} ")
            ->where([
                'shop_id' => $shop_id,
                'id' => $goods_id
            ]);
        Db::table('drh_goods')
            ->field(" goods_stock=goods_stock-{$goods_num} ")
            ->where([
                'proxy_goods_id' => $goods_id
            ]);
    }

    public function showAdminShop($status, $page, $pre, $sort, $recover_status = 0)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('drh_shop')
            ->field([
                'id',
                'shop_name',
            ])->where([
                'verify_status' => $status,
                'recover_status' => $recover_status,
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function verifyShop($shop_id, $status)
    {
        Db::table('drh_shop')
            ->where([
                'id' => $shop_id,
            ])->update([
                'verify_status' => $status
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    public function showWekOrder($shop_id)
    {
        $time = time();
        $time_start = date('Y-m-d', $time - 7 * 24 * 60 * 60);
        $time_end = date('Y-m-d', $time);
        return Db::table('calendar a')
            ->field([
                'a.date_list',
                "(select count(*) 
                from 
                drh_goods_order 
                where 
                FROM_UNIXTIME(order_end_time,'%Y-%m-%d') = a.date_list and shop_id={$shop_id}) 
                count"
            ])->group('a.date_list')
            ->where(" a.date_list >= '{$time_start}'  and a.date_list<='{$time_end}'")
            ->select();
    }

    public function showGoodsNumber($shop_id, $page = 1, $pre = 15, $is_shelves = 1)
    {
        $page = ($page - 1) * $pre;
        return Db::table('drh_goods a')
            ->field([
                'a.id',
                'a.goods_name',
                '( select count(*) from drh_goods_order where goods_id=a.id ) count',
                'a.goods_img'
            ])->where([
                'a.shop_id' => $shop_id,
                'a.is_shelves' => $is_shelves
            ])->group('a.id')
            ->order('count desc')
            ->limit($page, $pre)
            ->select();
    }

    public function showAnnexShop($page, $pre, $sort, $city_name, $recover_status = 0)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort, 'a.');
        return Db::table('drh_shop a')
            ->join('drh_shop_city b', 'a.id=b.shop_id', 'left')
            ->field([
                'a.id',
                'a.shop_name',
                'a.shop_logo',
            ])->where([
                'recover_status' => $recover_status,
                'b.city' => $city_name,
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showGoodNumber()
    {

    }

    public function searchShop($name, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('drh_shop')
            ->field([
                'id',
                'shop_name',
                'shop_logo',
                'shop_introduce',
            ])->where(" shop_name like '%{$name}%' and verify_status=1 and recover_status=0")
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }


    public function addBankCard($user_id, $user_name, $bank_name, $time, $bank_card_name, $card_number)
    {
        Db::table('drh_bank_card')
            ->insert([
                'user_id' => $user_id,
                'user_name' => $user_name,
                'bank_name' => $bank_name,
                'create_time' => $time,
                'card_number' => $card_number,
                'card_name' => $bank_card_name
            ]);
        return ['code' => $this->t_code, 'msg' => '添加成功'];
    }

    public function editBankCard($user_id, $user_name, $bank_name, $bank_card_name, $card_number)
    {
        Db::table('drh_bank_card')
            ->where([
                'user_id' => $user_id
            ])
            ->update([
                'card_number' => $card_number,
                'user_name' => $user_name,
                'bank_name' => $bank_name,
                'card_name' => $bank_card_name
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    public function showBankCardInfo($user_id)
    {
        return Db::table('drh_shop_bank_card')
            ->field([
                'card_number',
                'user_name',
                'bank_name'
            ])->where([
                'user_id' => $user_id
            ])->find();
    }

    public function getAnnexShopNumber($city_name)
    {
        $res = Db::table('drh_shop a')
            ->join('drh_shop_city b', 'a.id=b.shop_id', 'left')
            ->field('count(*) count')
            ->where([
                'b.city' => $city_name,
            ])->find();
        return $res['count'];
    }

    public function showShop($page, $pre, $sort, $verify_status)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('drh_shop')
            ->field([
                'id',
                'shop_name',
                'verify_status'
            ])->where([
                'verify_status' => $verify_status
            ])->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function showAdminShopInfo($id)
    {
        return Db::table('drh_shop')
            ->field([
                'shop_name',
                'shop_no',
                'recover_status',
                'integral',
                'money'
            ])->where([
                'id' => $id
            ])->find();
    }

    public function showProxyGoods($id, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('drh_goods')
            ->field([
                'goods_name',
                'id'
            ])->where("proxy_goods_id > 0 and shop_id={$id}")
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function addShopCity($shop_id, $city, $province, $city_id, $province_id,
                                $district_id, $district_name, $address, $longitude, $latitude,
                                $area_id, $area_name)
    {
        Db::table('drh_shop_city')
            ->insert([
                'shop_id' => $shop_id,
                'city' => $city,
                'province' => $province,
                'city_id' => $city_id,
                'province_id' => $province_id,
                'district_id' => $district_id,
                'district_name' => $district_name,
                'longitude' => $longitude,
                'latitude' => $latitude,
                'address' => $address,
                'area_id' => $area_id,
                'area_name' => $area_name,
            ]);
        return ['code' => $this->t_code, 'msg' => '添加成功'];
    }

    public function editShopCity($shop_id, $city, $province, $city_id, $province_id,
                                 $district_id, $district_name, $address, $longitude, $latitude,
                                 $area_id, $area_name)
    {
        Db::table('drh_shop_city')
            ->where([
                'shop_id' => $shop_id,
            ])
            ->update([
                'city' => $city,
                'province' => $province,
                'city_id' => $city_id,
                'province_id' => $province_id,
                'district_id' => $district_id,
                'district_name' => $district_name,
                'longitude' => $longitude,
                'latitude' => $latitude,
                'address' => $address,
                'area_id' => $area_id,
                'area_name' => $area_name,
            ]);
        return ['code' => $this->t_code, 'msg' => '修改成功'];
    }

    public function getInfo($user_open_id, $value, $whereName)
    {
        return Db::table('drh_shop a')
            ->join('drh_user_info b', 'a.user_id=b.id', 'left')
            ->field($value)
            ->where([
                $whereName => $user_open_id
            ])->find();
    }

    public function changIntegralMoney($shop_id, $shop_integral, $shop_money)
    {
        return Db::table('drh_shop')
            ->where([
                'id' => $shop_id
            ])->update([
                'integral' => $shop_integral,
                'money' => $shop_money,
            ]);
    }

    public function getWithdrawUserInfo($order_num)
    {
        return Db::table('drh_withdraw a')
            ->join('drh_shop b', 'a.user_id=b.user_id', 'left')
            ->field([
                'b.integral',
                'b.money',
                'b.id',
                'a.total_money',
                'a.user_id',
                'a.review_type'
            ])->where([
                'order_num' => $order_num
            ])->find();
    }

    public function showAllGoodsList($shop_id)
    {
        return Db::table('drh_goods')
            ->field([
                'id',
                'goods_name'
            ])->where([
                'shop_id' => $shop_id
            ])->select();
    }

    public function getPlatformIntegral($shop_id, $integral, $time, $order_num)
    {
        return Db::table('drh_shop_loan_integral')
            ->insertGetId([
                'order_num' => $order_num,
                'shop_id' => $shop_id,
                'integral' => $integral,
                'create_time' => $time
            ]);
    }

    public function showPlatformOrder($where, $page, $pre, $sort)
    {
        $page = ($page - 1) * $pre;
        $sort = Common::spliceArray($sort);
        return Db::table('drh_shop_loan_integral')
            ->field([
                'order_num',
                'shop_id',
                'integral',
                'verify_status',
                "FROM_UNIXTIME(create_time,'%Y-%m-%d %H:%i:%s') start_time",
                "FROM_UNIXTIME(end_time,'%Y-%m-%d %H:%i:%s') end_time"
            ])->where($where)
            ->order($sort)
            ->limit($page, $pre)
            ->select();
    }

    public function reviewShopPlatform($order, $shop_id, $status, $id, $end_time)
    {
        return Db::table('drh_shop_loan_integral')
            ->where([
                'order_num' => $order,
                'shop_id' => $shop_id,
                'id' => $id
            ])->update([
                'verify_status' => $status,
                'end_time' => $end_time
            ]);
    }

    public function addIntegralRatio($user_id, $shop_id, $value)
    {
        return Db::table('drh_shop')
            ->where([
                'user_id' => $user_id,
                'id' => $shop_id
            ])->update([
                'integral_return_ratio' => $value
            ]);
    }

    public function showIntegralRatio($user_id, $shop_id)
    {

        $res = Db::table('drh_shop')
            ->field([
                'integral_return_ratio'
            ])->where([
                'user_id' => $user_id,
                'id' => $shop_id
            ])->find();
        return empty($res['integral_return_ratio']) ? 0 : $res['integral_return_ratio'];
    }

    public function addQRCode($shop_id, $code)
    {
        return Db::table('drh_shop')
            ->where([
                'id' => $shop_id
            ])->update([
                'shop_qrcode' => $code
            ]);
    }

    public function showShopPay($shop_id, $user_id)
    {
        return Db::table('drh_shop')
            ->field([
                'shop_qrcode',
                'integral_return_ratio'
            ])->where([
                'id' => $shop_id,
                'user_id' => $user_id
            ])->find();
    }

    public function showShopPayInfo($shop_id, $user_id)
    {
        return Db::table('drh_shop')
            ->field([
                'id shop_id',
                'shop_name',
                'shop_logo',
                'integral_return_ratio'
            ])->where([
                'id' => $shop_id,
                'user_id' => $user_id
            ])->find();
    }

    public function showShopAnnex($right_bottom_lat, $left_top_lat, $left_top_lng, $right_bottom_lng)
    {
        return Db::table('drh_shop a')
            ->join('drh_shop_city b', 'a.id=b.shop_id', 'left')
            ->field([
                'b.shop_id',
                'a.shop_name',
                'a.shop_logo',
                'a.shop_introduce',
                'b.latitude',
                'b.longitude'
            ])->where("b.latitude <= {$left_top_lat} and b.latitude >= {$right_bottom_lat} 
            and 
            b.longitude <= {$right_bottom_lng} and b.longitude >= {$left_top_lng}")
            ->select();
    }

    public function updateShopCode($shop_id, $shop_qrcode)
    {
        return Db::table('drh_shop')
            ->where([
                'id' => $shop_id
            ])->update([
                'shop_qrcode' => $shop_qrcode
            ]);
    }
}